<?php
namespace Wz\Controller;
use Think\Controller;
use Think\Verify;            //使用验证码功能
use Think\Page;              //使用分页功能
use Think\Upload;            //使用上传功能

header("Content-type:text/html;charset=utf-8");
class IndexController extends Controller
{
    //用户注册
    public function _before_register()
    {
        if (IS_POST) {
            $u = D('username');
            $data = $u->create();
//            var_dump($data);
//            exit;
            $c['username'] = $data['username'];
            if ($data['username'] == '') {
                $this->error('用户名不能为空', U("register", 5));
            }
            if ($data['password'] == '') {
                $this->error('密码不能为空', U("register", 5));
            }
            if ($data['password'] !== I('post.password2')) {
                $this->error('两次密码不相同', U("register", 5));
            }
            if ($data['email'] == '') {
                $this->error('邮箱不能为空', U("email", 5));
            }
            if ($u->where($c)->count()) {
                $this->error("用户名已存在");
            }
            $u->add($data);
//            var_dump($u->add($data));exit;
            $vercode = $_POST['vercode'];
//            var_dump($vercode);
            $ver = new Verify();
            $u = $ver->check($vercode);
            if ($u) {
                $this->success("注册成功", U("/Wz/index/reg", 3));
            } else {
                $this->error('验证码错误');
            }
            exit;//不中断的话会执行后面的register方法里的display代码
        }
    }

    public function register()
    {
        $this->display();
    }

//用户登录
    public function _before_reg()
    {
        if (IS_POST) {
            $u = M('username');
            $data = $u->create();
//            var_dump($data);
//            exit;
            $userArr = $u->where($data)->find();
//            var_dump($userArr);
//            exit;
            if ($userArr) {
                session('username_id', $userArr['id']);//写凭证
                $this->success('登录成功', U('/Wz/index/shouye2'));
            } else {
                $this->error('用户名或密码不正确');
            }
            exit;
        }
    }

    public function reg()
    {
        $this->display();
    }


    public function save()
    {
        $article = M('shangping');           //1.参数，表名
        $data = $article->create();

        $upload = new Upload();
        $upload->mimes = array('image/png', 'image/gif', 'image/jpeg', 'image/jpg');
        $upload->maxSize = 1024 * 1024;
        $upload->exts = array('jpg', 'png', 'gif', 'png');
        $upload->savePath = 'image2/';        //保存到指定文件夹中

        $r = $upload->uploadOne($_FILES['phone']);
        $file = $r['savepath'] . $r['savename'];

        $u = M('shangping');
        $data['phone'] = $file;              //字段 photo   =  文件字符串            赋值

        if (!$file == "") {
            $k = 1;             //把文件字符串传进数据库中
            if ($k) {
                $this->success('上传成功', U('/Wz/index/shouye'), 3);
            } else {
                $this->error('上传失败');
            }
        } else {
            $this->error('图片不能为空');
        }

        if (!$data['sp_ming'] == "") {
            if (!$data['sp_id'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
                        $this->success("数据添加成功", U('/Wz/Index/shouye'), 3);
                    } else {
                        $this->error("发布失败");
                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }
    }


    public function shouye()
    {
        $article = M("shangping");                //数据表名
        $p = I("get.page");

        $totolCount = $article->count();
        $pager = new Page($totolCount, 15);

        $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
        $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');


        $data = $article->page($p, 15)->select();
        $this->assign('lists', $data);

        $this->assign('pager', $pager);

        $this->display();
    }

    public function shouye2()
    {
        $article = M("username");
        $id = $_GET['id'];
        $data = $article->find($id);
        $this->assign('blog', $data);


        $article = M("shangping");                //数据表名
        $p = I("get.page");

        $totolCount = $article->count();
        $pager = new Page($totolCount, 15);

        $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
        $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');


        $data = $article->page($p, 15)->select();
        $this->assign('lists', $data);

        $this->assign('pager', $pager);

        $this->display();
    }


    public function vercode()
    {
        $ver = new Verify();
        $ver->entry();
    }


    public function xiangqing()
    {
        $article = M("shangping");
        $id = $_GET['id'];
        $data = $article->find($id);
        $this->assign('blog', $data);

        $this->display();
    }


    public function save2()
    {
        $article = M('chanpin');           //1.参数，表名
        $data = $article->create();
        if (!$data['cp_changming'] == "") {
            if (!$data['cp_cd'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
                        $this->success("数据添加成功", U('/Wz/Index/'), 3);
                    } else {
                        $this->error("发布失败");
                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }
    }

    //管理员登录
    public function _before_Admin(){
        if (IS_POST){
            $u = M('guangliyuan');
            $data = $u->create();
//            var_dump($data);
//            exit;
            $userArr = $u->where($data)->find();
//            var_dump($userArr);
//            exit;
            if ($userArr){
                session('username_id',$userArr['id']);//写凭证
                $this->success('登录成功',U('shouye'));
            } else {
                $this->error('用户名或密码不正确');
            }
            exit;
        }
    }

    public function Admin(){
        $this->display();
    }

    public function Addingdan()
    {
        $nowTime = time();
        $this->assign('nowTime', $nowTime);

//        $article = M("");
//        $id = $_GET['id'];
//        $data = $article->find($id);
//        $this->assign('blog', $data);

        $this->display();
    }



    public function dizhi(){

        $article = M('yhdizhi');           //1.参数，表名
        $data = $article->create();
        var_dump($data);

        if (!$data['yh_sheng'] == "") {
            if (!$data['yh_shi'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
                        $this->success("数据添加成功", U('/Wz/Index/site'), 3);
                    } else {
                        $this->error("发布失败");
                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }
    }

     public function site(){
         $article = M("yhdizhi");                //数据表名
         $p = I("get.page");

         $totolCount = $article->count();
         $pager = new Page($totolCount, 6);

         $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
         $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');

         $data = $article->page($p, 6)->select();
         $this->assign('lists', $data);

         $this->assign('pager', $pager);

         $this->display();
     }


     public function daizhifu(){
         $article = M("yhdizhi");                //数据表名
         $p = I("get.page");

         $totolCount = $article->count();
         $pager = new Page($totolCount, 6);

         $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
         $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');


         $data = $article->page($p, 6)->select();
         $this->assign('lists', $data);

         $this->assign('pager', $pager);

         $this->display();
     }

    public function deldizhi(){
        $article = D('yhdizhi');
        $id=$_GET['id'];
        $f=$article->find($id);

        if ($f) {
            $r = $article->delete();
            if ($r) {
                $this->success("数据删除成功",U('/wz/Index/site'),3);
            } else {
                $this->error("删除失败");
            }
        } else{
            echo "sorry";
        }
    }


    public function xiugaidizhi(){
         $article = M("yhdizhi");
         $id = $_GET['id'];
         $data = $article->find($id);
         $this->assign('blog', $data);

         $this->display();
     }

    public function xgdizhi(){
        $article = M('yhdizhi');
        $f = $article->create();
        var_dump($f);
        if(!$f['yh_sheng']==""){
            if(!$f['yh_shi']==""){
                if ($f){
                    $r = $article->save();
                    var_dump($r);
                    if ($r) {
                        $this->success("数据添加成功",U('/wz/Index/daizhifu'),3);
                    } else {
                        $this->error("发布失败");
                    }
                } else{
                    echo "sorry";
                }
            }else{
                $this->error('内容不能为空');
            }
        }else{
            $this->error('标题不能为空');
        }
    }
































//最底的括号不要动
}






